/**
 * 
 */
package com.skyme.automation.generate.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.internal.Workbench;

import com.skyme.automation.generate.GeneratePlugin;

/**
 * 工具类
 * 
 * @author zhangfeng
 * 
 */
public class Tools {

	/**
	 * 将指定的URL拷贝到指定的工程中的指定目录
	 * 
	 * @param url
	 *            指定的URL
	 * @param project
	 *            指定的工程
	 * @param destFileName
	 *            工程中的指定目录
	 */
	public static void copyURLToFile(URL url, IProject project,
			String destFileName) {
		InputStream inStream = null;
		try {
			inStream = url.openStream();
			IFile file = project.getFile(destFileName);
			if (!file.exists()) {
				file.create(inStream, true, null);
			}
		} catch (IOException e) {
			GeneratePlugin.log(e);
		} catch (CoreException e) {
			GeneratePlugin.log(e);
		} finally {
			try {
				if (inStream != null)
					inStream.close();
			} catch (IOException e) {
				GeneratePlugin.log(e);
			}
		}
	}

	/**
	 * 得到eclipse的当前工程
	 * 
	 * @return
	 */
	public static IProject getCurrentProject() {
		ISelectionService selectionService = Workbench.getInstance()
				.getActiveWorkbenchWindow().getSelectionService();

		ISelection selection = selectionService.getSelection();

		IProject project = null;
		if (selection instanceof IStructuredSelection) {
			Object element = ((IStructuredSelection) selection)
					.getFirstElement();

			if (element instanceof IResource) {
				project = ((IResource) element).getProject();
			} else if (element instanceof PackageFragmentRootContainer) {
				IJavaProject jProject = ((PackageFragmentRootContainer) element)
						.getJavaProject();
				project = jProject.getProject();
			} else if (element instanceof IJavaElement) {
				IJavaProject jProject = ((IJavaElement) element)
						.getJavaProject();
				project = jProject.getProject();
			}
		}
		return project;
	}

	/**
	 * 在工程上创建文件夹
	 * 
	 * @param project
	 *            工程
	 * @param pathName
	 *            文件夹名称
	 */
	public static void createPackageFloder(IProject project, String floderName) {
		IFolder folder = project.getFolder(floderName);
		if (!folder.exists()) {
			try {
				folder.create(true, true, null);
			} catch (CoreException e) {
				GeneratePlugin.log(e);
			}
		}
	}

	/**
	 * 得到eclipse的版本号
	 * 
	 * @return
	 */
	public static String getEclipseVersion() {
		return System.getProperty("osgi.framework.version");
	}

	/**
	 * 从文件路径得到输入流
	 * 
	 * @param path
	 * @return
	 */
	public static InputStream fileInput(File path) {
		try {
			FileInputStream fis = new FileInputStream(path);
			return fis;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 向工程中添加指定的文件
	 * @param container
	 * @param path
	 * @param contentStream
	 * @param monitor
	 * @throws CoreException
	 */
	public static void addFileToProject(IContainer container, Path path,
			InputStream contentStream, IProgressMonitor monitor)
			throws CoreException {
		final IFile file = container.getFile(path);

		if (file.exists()) {
			file.setContents(contentStream, true, true, monitor);
		} else {
			file.create(contentStream, true, monitor);
		}

	}

}
